package com.ha.springcloud.controller;


import com.ha.springcloud.pojo.Department;
import com.ha.springcloud.pojo.Employee;
import com.ha.springcloud.service.DepartmentService;
import com.ha.springcloud.service.EmployeeService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//实现页面跳转不能使用restcontroller
@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DepartmentService departmentService;

//    @RequestMapping("/queryEmployeeList")
//    public String  queryemployeeList(){
//        return "hello";
//    }
    @RequestMapping("/queryEmployeeList")
    public List<Employee> queryemployeeList(){
        System.out.println("provider");
        List<Employee> employeeList =employeeService.queryEmployeeList();
        return employeeList;
    }

    @HystrixCommand(fallbackMethod = "HystrixGet")//服务访问失败所调用的方法
    @RequestMapping("/emp/get/{id}")
    public Employee get(@PathVariable("id") String id){
        Employee employee = employeeService.queryEmployeeById(id);

        if(employee==null){
            throw new RuntimeException("id=>"+id+"不存在，或者信息无法访问");//只会在后台显示信息，前台页面还是500错误
        }
        return employee;
    }

    public Employee HystrixGet(@PathVariable("id") String id){
        return new Employee().setId(id).
                setName("id=>"+id+"不存在，或者信息无法访问--Hystrix");
    }


    //展示所有员工信息
    @RequestMapping("/emps")
    public String list(Model model){
        System.out.println("hi");
        List<Employee> employeeList =employeeService.queryEmployeeList();
        model.addAttribute("emps",employeeList);
        return "emp/list";
    }

    @RequestMapping("/add")
    public String add(Model model){
        //查询出所有部门的信息
        List<Department> departments = departmentService.queryDepartmentList();
        model.addAttribute("departments",departments);

        return "emp/add";
    }

    @PostMapping("/emp")
    public String addEmp(Employee employee){
        //添加的操作
        int maxId = employeeService.getMaxId();
        String id=""+maxId;
        employee.setId(id);
        employeeService.addEmployee(employee);
        return "redirect:/emps";
    }

    @GetMapping("/update/{id}")
    public String updateEmp(@PathVariable("id")String id, Model model){
        Employee employee = employeeService.queryEmployeeById(id);
        model.addAttribute("emp",employee);
        List<Department> departments = departmentService.queryDepartmentList();
        model.addAttribute("departments",departments);
        return "emp/update";
    }

    @PostMapping("/updateEmp")
    public String updateEmp(Employee employee){
        System.out.println(employee);
        //添加的操作
        String id=employee.getId();
        employeeService.updateEmployee(employee);
        return "redirect:/emps";
    }

    @GetMapping("/delete/{id}")
    public String deleteEmp(@PathVariable("id")String id){

        employeeService.deleteEmployee(id);
        return "redirect:/emps";
    }

}
